//
//  CRSpaceCell.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/21.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRSpaceCell.h"
#import "CRSpace.h"
#import "CRSpaceFrame.h"
#import "UIImageView+WebCache.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#import "CRCellBottomView.h"
#import "CRCommentTextField.h"
#import "UIImageView+WebCache.h"
#import "UIImageView+WebCache.h"

#define margin 5
// 显示赞的人的username的label的文字大小
#define praiseInfoFont [UIFont systemFontOfSize:12]
#define nickfoFont [UIFont boldSystemFontOfSize:15]

@interface CRSpaceCell ()

/**
 *  头像
 */
@property (nonatomic, strong) UIImageView *icon;

/**
 *  昵称
 */
@property (nonatomic, strong) UILabel *nickName;

/**
 *  正文
 */
@property (nonatomic, strong) UILabel *content;

/**
 *  发送的头像
 */
@property (nonatomic, strong) UIImageView *image;

/**
 *  时间
 */
@property (nonatomic, strong) UILabel *timeLabel;


/**
 *  存放图片的数组
 */
@property (nonatomic, strong) NSMutableArray *pics;

/**
 *  评论条
 */
//@property (nonatomic, strong) CRCommentTextField *conmentTextField;


/**
 *  显示赞的人的username
 */
@property (nonatomic, strong) UILabel *praiseInfo;

/**
 *  赞的人前面的小图标
 */
@property (nonatomic, strong) UIButton *praisImg;





@end

@implementation CRSpaceCell

- (NSMutableArray *)pics
{
    if (!_pics) {
        _pics = [NSMutableArray array];
    }
    return _pics;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"space_bg"]];
        self.backgroundView.layer.cornerRadius = 10;
        self.backgroundView.clipsToBounds = YES;

        
        // 添加控件
        self.icon = [[UIImageView alloc] init];
        self.icon.layer.cornerRadius = 15;
        self.icon.clipsToBounds = YES;
        self.nickName = [[UILabel alloc] init];
        self.nickName.font = nickfoFont;
        self.nickName.textColor = [UIColor orangeColor];
        self.content = [[UILabel alloc] init];
        // 换行
        self.content.numberOfLines = 0;
        self.content.font = [UIFont systemFontOfSize:14];
        self.image = [[UIImageView alloc] init];
        
         [self.image addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTap:)]];
        self.image.tag = 0;
        self.image.userInteractionEnabled = YES;
        [self.pics addObject:self.image];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:10];
        self.timeLabel.textAlignment = NSTextAlignmentRight;
        self.bottomView = [[CRCellBottomView alloc] init];
        
        self.praiseInfo = [[UILabel alloc] init];
        self.praiseInfo.font = praiseInfoFont;
        self.praiseInfo.textColor = [UIColor blueColor];
//        self.conmentTextField = [[CRCommentTextField alloc] init];
//        self.conmentTextField.placeholder = @" 请输入评论内容...";
//        self.conmentTextField.font = [UIFont systemFontOfSize:13];
        
        self.praisImg = [[UIButton alloc] init];
        self.praisImg.userInteractionEnabled = NO;
        [self.praisImg setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-1"] forState:UIControlStateNormal];
    
  
        
        [self.contentView addSubview:self.icon];
        [self.contentView addSubview:self.nickName];
        [self.contentView addSubview:self.image];
        [self.contentView addSubview:self.content];
        [self.contentView addSubview:self.timeLabel];
        [self.contentView addSubview:self.bottomView];
        [self.contentView addSubview:self.praiseInfo];
        [self.contentView addSubview:self.praisImg];
//        [self.contentView addSubview:self.conmentTextField];

    }
    return self;
}

- (void)photoTap:(UITapGestureRecognizer *)recognizer
{

    int count = (int)self.pics.count;
    
    // 1.封装图片数据
    NSMutableArray *myphotos = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++) {
        // 一个MJPhoto对应一张显示的图片
        MJPhoto *mjphoto = [[MJPhoto alloc] init];
        
        mjphoto.srcImageView = self.pics[i]; // 来源于哪个UIImageView
        
        [myphotos addObject:mjphoto];
    }
    
    // 2.显示相册
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.currentPhotoIndex = recognizer.view.tag; // 弹出相册时显示的第一张图片是？
    browser.photos = myphotos; // 设置所有的图片
    [browser show];
}


- (void)setSpaceFrame:(CRSpaceFrame *)spaceFrame
{
    _spaceFrame = spaceFrame;
    
   
    // 设置数据
    [self setupData];
    // 设置frame
    [self setupFrame];
    
    
    
}

- (void)setupData
{
    
    CRSpace *space = _spaceFrame.space;

    [self.icon sd_setImageWithURL:[NSURL URLWithString:space.iconUrl]];
    self.nickName.text = space.nickName;
    self.content.text = space.content;
    
    self.bottomView.praise.text = [NSString stringWithFormat:@"%d", space.likeCount];
    self.bottomView.noPraise.text = [NSString stringWithFormat:@"%d", space.noLickCount];
    self.bottomView.share.text = [NSString stringWithFormat:@"%d", space.shareCount];
    self.bottomView.address.text = space.address;
    if (space.address.length) {
        self.bottomView.addressIcon.image = [UIImage imageNamed:@"iconfont-tongcheng"];
        self.bottomView.addressIcon.hidden = NO;
    } else {
        self.bottomView.addressIcon.hidden = YES;
    }
    
    if (self.spaceFrame.space.picurl) {
        self.image.hidden = NO;
        [self.image sd_setImageWithURL:[NSURL URLWithString:self.spaceFrame.space.picurl]];
    } else {
        self.image.hidden = YES;
    }
    
    
    if (!space.praiseInfo.count) { // 没有赞的人
        self.praiseInfo.hidden = YES;
        self.praisImg.hidden = YES;
    } else {
        self.praiseInfo.hidden = NO;
        self.praisImg.hidden = NO;
        NSArray *strArray = space.praiseInfo;
        NSMutableString *muStr = [NSMutableString string];
        for (NSString *str in strArray) {
            [muStr appendFormat:@"%@、",str];
        }
        NSString *strinfo = [NSString stringWithFormat:@"%@等人觉得很赞", muStr];
        self.praiseInfo.text = strinfo;
    }
    
    
    
    self.timeLabel.text = space.timeLabel;

    
    
}

- (void)setupFrame
{
    CRSpace *space = _spaceFrame.space;
    self.icon.frame = _spaceFrame.iconF;
    self.nickName.frame = _spaceFrame.nickNameF;
    self.content.frame = _spaceFrame.contentF;
    self.image.frame = _spaceFrame.imageF;
    
    self.timeLabel.frame = _spaceFrame.timeLabelF;
    self.timeLabel.x = self.width - self.timeLabel.width - 10;
    
    self.bottomView.frame = _spaceFrame.bottomViewF;
//    self.conmentTextField.frame = _spaceFrame.commentTextF;
    
    // 需要动态计算----用于显示赞的人的frame
    // 显示赞的人的username的label的frame
    self.praiseInfo.frame = _spaceFrame.praiseInfoF;
    

    NSArray *strArray = space.praiseInfo;
    NSMutableString *muStr = [NSMutableString string];
    for (NSString *str in strArray) {
        [muStr appendFormat:@"%@、",str];
    }
    NSString *strinfo = [NSString stringWithFormat:@"%@等人觉得很赞", muStr];

    // 在此动态计算赞的人的label的大小
    CGSize size = [self sizeWithText:strinfo andMaxSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 6 * margin, MAXFLOAT) andFont:praiseInfoFont];
    CGFloat praiseInfoH = size.height;
    CGRect frame = self.praiseInfo.frame;
    frame.size.height = praiseInfoH;
    self.praiseInfo.frame = frame;
   
    self.praisImg.frame = _spaceFrame.praiseF;

    
}

// 重新设置cell的frame
- (void)setFrame:(CGRect)frame
{
  
    frame.origin.y += margin;
    frame.origin.x = margin;
    frame.size.width -= 2 * margin;
    frame.size.height -= margin;
    
    
    
    [super setFrame:frame];
}

#pragma mark - KVO space的likeCount的值改变的时候调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    self.bottomView.praise.text = [NSString stringWithFormat:@"%d", self.spaceFrame.space.likeCount];
    self.bottomView.noPraise.text = [NSString stringWithFormat:@"%d", self.spaceFrame.space.noLickCount];
    self.bottomView.share.text = [NSString stringWithFormat:@"%d", self.spaceFrame.space.shareCount];


}


// 根据给定的字符串 最大值的size 给定的字体 来计算文字应该占用的大小
- (CGSize)sizeWithText:(NSString *)text andMaxSize:(CGSize)maxSize andFont:(UIFont *)font
{
    NSDictionary *attr = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size;
}



@end











